function repeat(str){
    let index = 0
    const strStack = []
    const numberStack = []
    let rest = str
    while(index < str.length - 1){
        rest = str.substr(index)
        if(/^\d+\[/.test(rest)){
            const numberStr = rest.match(/^(\d+)\[/)[1]
            numberStack.push(Number(numberStr))
            strStack.push('')
            index += numberStr.length + 1
        }else if(/^\w+\]/.test(rest)){
            const str =  rest.match(/^(\w+)\]/)[1]
            strStack[strStack.length - 1] = str
            index += str.length
        }else if(rest[0] === ']'){
            const number = numberStack.pop()
            const str = strStack.pop()
            strStack [strStack.length - 1] += str.repeat(number)
            index ++
        }
    }
    return strStack[0].repeat(numberStack[0])
}

console.log(repeat('2[1[a]3[bee]2[3[c]4[d]]]'))